package com.wanka.jutou.config;

import com.sun.istack.internal.NotNull;
import com.wanka.jutou.RuleType;

/**
 * @author yangqing
 * @version 1.0
 * @date 2025/9/1 17:21
 * @description
 */

public class AbstractDataSourceProperties {
    private @NotEmpty String dataType = "json";
    private @NotNull RuleType ruleType;
    private String converterClass;
    @JsonIgnore
    private final String factoryBeanName;
    @JsonIgnore
    private Environment env;

    public AbstractDataSourceProperties(String factoryBeanName) {
        this.factoryBeanName = factoryBeanName;
    }

    public String getDataType() {
        return this.dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public RuleType getRuleType() {
        return this.ruleType;
    }

    public void setRuleType(RuleType ruleType) {
        this.ruleType = ruleType;
    }

    public String getConverterClass() {
        return this.converterClass;
    }

    public void setConverterClass(String converterClass) {
        this.converterClass = converterClass;
    }

    public String getFactoryBeanName() {
        return this.factoryBeanName;
    }

    protected Environment getEnv() {
        return this.env;
    }

    public void setEnv(Environment env) {
        this.env = env;
    }

    public void preCheck(String dataSourceName) {
    }

    public void postRegister(AbstractDataSource dataSource) {
        switch (this.getRuleType()) {
            case FLOW:
                FlowRuleManager.register2Property(dataSource.getProperty());
                break;
            case DEGRADE:
                DegradeRuleManager.register2Property(dataSource.getProperty());
                break;
            case PARAM_FLOW:
                ParamFlowRuleManager.register2Property(dataSource.getProperty());
                break;
            case SYSTEM:
                SystemRuleManager.register2Property(dataSource.getProperty());
                break;
            case AUTHORITY:
                AuthorityRuleManager.register2Property(dataSource.getProperty());
                break;
            case GW_FLOW:
                GatewayRuleManager.register2Property(dataSource.getProperty());
                break;
            case GW_API_GROUP:
                GatewayApiDefinitionManager.register2Property(dataSource.getProperty());
        }

    }
}
